﻿namespace PINGStance

module BucketFunctions =

    open System
    open System.IO
    open System.Threading

    let write_log_file( section : string,
                        msg : string,
                        file_append : bool,
                        msg_to_console : bool,
                        lock : System.Threading.ReaderWriterLock
                        ) = 

        let log_line = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "," + section + "," + msg

        try 
            lock.AcquireWriterLock(5000)

            use log_file = new System.IO.StreamWriter( "pingstance.log", file_append)

            log_file.WriteLine( log_line ) |> ignore

            log_file.Close() |> ignore

            log_file.Dispose()

            lock.ReleaseWriterLock()

        with
            | _ as ex -> failwith( ex.Message )


        if msg_to_console then
            printfn "%s" log_line

